import sys
import time
import ok
from PyQt4 import QtGui, QtCore
from dialog import *


class MyThread(QtCore.QThread):
    countChange = QtCore.pyqtSignal(int)
    countReset  = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(MyThread, self).__init__(parent)
        self.stopped = QtCore.QEvent(QtCore.QEvent.User)

    def start(self):
        self.stopped.setAccepted(False)
        self.count = 0

        super(MyThread, self).start()

    def run(self):
        while not self.stopped.isAccepted():
            xem.UpdateWireOuts()
            self.count = xem.GetWireOutValue(0x20)
            self.countChange.emit(self.count)
            time.sleep(0.1)

        self.countReset.emit(0)

    def stop(self):
        self.stopped.setAccepted(True)

class GUIForm(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)

        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.pushButton_Load, QtCore.SIGNAL('clicked()'), self.Load)
        
        self.thread = MyThread(self)
        self.thread.countChange.connect(self.ui.lcdNumber.display)
        self.thread.countChange.connect(self.ui.lcdNumber.display)
        self.thread.start()

    def Load(self):
        xem.ActivateTriggerIn(0x40,0)


xem = ok.FrontPanel()

xem.OpenBySerial(xem.GetDeviceListSerial(0))
app = QtGui.QApplication(sys.argv)
myapp = GUIForm()
myapp.show()
sys.exit(app.exec_())
